/*
 * 
 *
 * Authors: Soeren Gebbert, soeren.gebbert@vti.bund.de
 *          Axel Don, axel.don@vti.bund.de
 *
 * Copyright:
 *
 * Johann Heinrich von Thünen-Institut
 * Institut für Agrarrelevante Klimaforschung
 *
 * Phone: +49 (0)531 596 2601
 *
 * Fax:+49 (0)531 596 2699
 *
 * Mail: ak@vti.bund.de
 *
 * Bundesallee 50
 * 38116 Braunschweig
 * Germany
 *
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 2 of the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

#include <iostream>
#include <sstream>
#include <fstream>
#include "permutation_event.h"
#include "micr_cluster.h"
#include "carb_particle.h"

using namespace std;

/* -------------------------------------------------------------------------- */
permutation_event::permutation_event() : event_base() {
    this->depth = 0;
}

/* -------------------------------------------------------------------------- */
permutation_event::permutation_event(std::string filename) : event_base() {
    this->depth = 0;
    this->filename = filename;
    this->read_parameter();
}

/* -------------------------------------------------------------------------- */
bool permutation_event::read_parameter(){
    
    char buff[1024];
    size_t pos;

    ifstream input(this->filename.c_str(), ifstream::in);
    
    this->read_base_parameter(input);
    
    // Rewind the input file stream
    input.close();
    input.open(this->filename.c_str(), ifstream::in);
    
    if (input) {
        while (input.getline(buff, 1024)) {

            string line(buff);
            string sub;
            /* Ignore lines with a leading #, its a comment */
            if (line.find("#") != string::npos)
                continue;

            if (line.find("depth") != string::npos) {
                pos = line.find("=");
                if (pos != string::npos) {
                    sub = line.substr(pos + 1);
                    stringstream ss(sub);
                    ss >> this->depth;
                }
            }
        }
    } else {
        cerr << "Unable to read event parameter file " << this->filename << endl;
        return false;
    }
    
    input.close();
    
    return true;
}

/* -------------------------------------------------------------------------- */
bool permutation_event::modify_particles(unsigned int timestep, initial_parameters *init_params, 
                                  runtime_parameters *run_params,
                                  std::vector<micr_cluster*> &mc, 
                                  std::vector<carb_particle*> &cp)
{
    unsigned int i;
    double x, y, z;
    
    if(timestep < this->start)
        return true;
    
    if(timestep > this->stop)
        return true;
    
    if(timestep != this->start + this->count * this->frequence)
        return true;
    
    cout << "Running permutation event at timestep " << timestep << endl;
    
    for(i = 0; i < mc.size(); i++) {
        
        if(mc[i]->get_z() >= this->depth) {
            x = (float) drand48() * init_params->width;
            y = (float) drand48() * init_params->height;
            z = (float) drand48() * this->depth;
            mc[i]->set_x(x);
            mc[i]->set_y(y);
            mc[i]->set_z(z);
        }
    }
    
    for(i = 0; i < cp.size(); i++) {
        if(cp[i]->get_z() >= this->depth) {
            x = (float) drand48() * init_params->width;
            y = (float) drand48() * init_params->height;
            z = (float) drand48() * this->depth;
            cp[i]->set_x(x);
            cp[i]->set_y(y);
            cp[i]->set_z(z);
        }
    }
    
    this->count++;
    
    return true;
}